﻿using GSOrganization.Application;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System.Security.Claims;

namespace Furion.Web.Entry.Controllers;
[Authorize(AuthenticationSchemes = Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationDefaults.AuthenticationScheme)]
public class SysPersonController : BaseController
{

    private RSACrypto rsaCrypto = new RSACrypto(@"MIICXAIBAAKBgQCLqzqTeRC4uoRx2R61hda7Y82RTKH2bWlgWigXaz3t337p3z47
jJ4c/klQOIY5XTcxf1n+nzyd5QdJLy7KX76Izg0MMRNg8TIzjFyQqbh6P2Hx6zqh
0N9SSIF9HvUcJ7XuzMQc1E1te0d9gLOG7pO58l3ar8UYG0t3uLnqlC64mwIDAQAB
AoGAEip3N+8rKZu41kXzJJD/2OemORvKhQ6+AfY3+LPOHj5Pd4O9yGkFt6erSW/Q
pW1mXF3ACsoTTEYZqH8mitPhKCDIwOiAlMQjezIjKJQoNXMNNKpfWaJRyjfCOWiK
ppvIw4PTep5bh+c3RvSbMvDvnwcKsj9NfZICQ3QR5krIXeECQQC1GuChUk/d53T+
+g4lLgP0WlyW4s5il4m05B8rnVrHjeOSvOEg8ACDU+DonTWnZzkYwro63RiK0/Bk
Uu063fI3AkEAxW2dScr/3/0KEPpk/GiJaotgPnqLLoIH9aDvlj07WyE3qFbOkPFc
EVLuSXTf3rPkeqaAJC+SBP/VdxRjcKlKvQJASpesZ6WQcbRwcsipZNTkGhPRyO/X
07HYrSG8WOlxx3mAfyK+kyNnArBclVKTE8UnNjSWIcKI5j1O6PNi9HkP1wJAHMmC
6cOj07E9yoP4PaWwg8yW2AVeTgW/y10QEkQTZ5JMX0tpC1FmL1B8VlGoYvywEAI/
JQearwfuxFJAvaixsQJBAKcxZUK8elewFTASWyLBDjS7fDSJ6om/tU18XJPQvD6E
KJIrtRvGx1LB6CHvJQ1NiPNQkTqZLIQLRu9KRf9nPD4=", @"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCLqzqTeRC4uoRx2R61hda7Y82R
TKH2bWlgWigXaz3t337p3z47jJ4c/klQOIY5XTcxf1n+nzyd5QdJLy7KX76Izg0M
MRNg8TIzjFyQqbh6P2Hx6zqh0N9SSIF9HvUcJ7XuzMQc1E1te0d9gLOG7pO58l3a
r8UYG0t3uLnqlC64mwIDAQAB");
    private readonly ISysPersonService _sysPersonService;

    public SysPersonController(ISysPersonService sysPersonService, IHttpContextAccessor httpContextAccessor)
    {

        _sysPersonService = sysPersonService;

    }

    /// <summary>
    /// 用户管理
    /// </summary>
    /// <returns></returns>
    public IActionResult Index()
    {


        return View();
    }
    /// <summary>
    /// 列表
    /// </summary>
    /// <returns></returns>
    public IActionResult DataSysPerson([FromBody] SysPersonIndexInput param)
    {
        var data = _sysPersonService.DataSysPerson(param);
        return Json(data);
    }
    /// <summary>
    /// 创建
    /// </summary>
    /// <returns></returns>
    public IActionResult Create()
    {
        return View();
    }
    /// <summary>
    /// 编辑
    /// </summary>
    /// <returns></returns>
    public IActionResult Edit(string id)
    {
        var data = _sysPersonService.GetSysPerson(id);

        return View(data);
    }
    /// <summary>
    /// 编辑
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    public IActionResult EditSysPerson([FromBody] SysPersonInput entity)
    {

        var data = _sysPersonService.CreateSysPerson(entity);

        return Json(data);
    }
    /// <summary>
    /// 删除
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    public IActionResult DeleteSysPerson(string id)
    {
        var sdaf = UserId;
        var data = _sysPersonService.DeleteSysPerson(id);

        return Json(data);
    }
    /// <summary>
    /// 重置密码
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    public IActionResult ResetSysPerson(string id)
    {

        var data = _sysPersonService.ResetSysPerson(id);

        return Json(data);
    }
    /// <summary>
    /// 详情
    /// </summary>
    /// <returns></returns>
    public IActionResult Detail()
    {
        return View();
    }
    /// <summary>
    /// 登录
    /// </summary>
    /// <param name="param">登录参数</param>
    /// <returns></returns>
    [HttpPost]
    [AllowAnonymous]
    public JsonResult UserLogin([FromBody] LoginInput param)
    {
        MyCurrentUser userdata = null;
        try
        {
            if (param == null)
            {
                return Json(new { StatusCode = "500", Errors = "对象不存在" });
            }

            param.LoginName = rsaCrypto.Decrypt(param.LoginName);
            param.PassWord = rsaCrypto.Decrypt(param.PassWord);

       
            userdata = _sysPersonService.GetCurrentUser(param);

            if (userdata == null)
            {
                return Json(new { StatusCode = "500", Errors = "用户名或者密码不准确" });
            }

            #region 下面是支持mvc客户端的cookie功能

            //3.配置认证属性 比如过期时间，是否持久化。。。。
            var authProperties = new AuthenticationProperties
            {

                IsPersistent = false,
                //持久化 ，比如 登录的时候 勾选记住我 复选框
            };

            ClaimsIdentity identity = new ClaimsIdentity("Forms");

            //用户编号
            identity.AddClaim(new Claim(ClaimTypes.Sid, userdata.Id));

            //用户名称
            identity.AddClaim(new Claim(ClaimTypes.Name, userdata.UserName));
            // HttpContext.Session.SetString("name", userdata.Name);

            identity.AddClaim(new Claim(ClaimTypes.Role, userdata.RoleName));

            var userjson = JsonConvert.SerializeObject(userdata);
            identity.AddClaim(new Claim(ClaimTypes.UserData, userjson));

            ClaimsPrincipal claimsPrincipal = new ClaimsPrincipal(identity);

            HttpContext.SignInAsync(Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationDefaults.AuthenticationScheme, claimsPrincipal, authProperties);

            #endregion
        }
        catch (Exception ex)
        {
            return Json(new { StatusCode = "400", Errors = ex.Message });
        }

        return Json(new { StatusCode = "200", Data = userdata });
    }
    [HttpGet]
    public ActionResult ChangePassword()
    {


        return View();

    }
    [HttpGet]
    public ActionResult Setting()
    {

        var data = _sysPersonService.SettingSysPerson();

        return View(data);


    }
    /// <summary>
    /// 修改密码
    /// </summary>
    /// <param name="entity">修改密码的模型</param>
    /// <returns></returns>
    [HttpPost]
    public ActionResult Setting([FromBody] SysPersonSettingInput param)
    {
        if (param == null)
        {
            return Json(new { StatusCode = "500", Errors = "对象不存在" });
        }
        //验证密码
        if (string.IsNullOrWhiteSpace(param.LoginName))
        {
            return Json(new { StatusCode = "500", Errors = "登录使用的用户名不能为空" });
        }
        if (string.IsNullOrWhiteSpace(param.ContactPerson))
        {
            return Json(new { StatusCode = "500", Errors = "姓名不能为空" });
        }
        if (string.IsNullOrWhiteSpace(param.ContactPhone))
        {
            return Json(new { StatusCode = "500", Errors = "电话不能为空" });
        }


        var data = _sysPersonService.Setting(param);

        return Json(data);
    }
    /// <summary>
    /// 修改密码
    /// </summary>
    /// <param name="entity">修改密码的模型</param>
    /// <returns></returns>
    [HttpPost]
    public ActionResult ChangePassword([FromBody] ChangePasswordInput param)
    {
        if (param == null)
        {
            return Json(new { StatusCode = "500", Errors = "对象不存在" });
        }
       
        param.OldPassword = rsaCrypto.Decrypt(param.OldPassword);
        param.NewPassword = rsaCrypto.Decrypt(param.NewPassword);
        param.ConfirmPassword = rsaCrypto.Decrypt(param.ConfirmPassword);

    
        var data = _sysPersonService.ChangePassword(param);

        return Json(data);
    }
    
}